#region Imports

using System;
using IEC.ColumnOrdering.SP2007.Application.Domain;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;

#endregion

namespace IEC.ColumnOrdering.SP2007.Application.Services.Concrete
{

    /// <summary>
    /// Provides functionality to convert applications.
    /// </summary>
    internal static class ApplicationConverter
    {

        #region Constants

        #endregion

        #region Methods - Static Member

        /// <summary>
        /// Converts the given SharePoint application collection to a collection of applications.
        /// </summary>
        /// <param name="sharepointApplicationCollection">
        /// The SharePoint application collection to be converted.
        /// </param>
        /// <returns>
        /// The result of converting <paramref name="sharepointApplicationCollection"/>.
        /// </returns>
        public static ApplicationCollection Convert(
            SPWebApplicationCollection sharepointApplicationCollection)
        {
            ApplicationCollection applicationCollection = new ApplicationCollection();

            foreach (SPWebApplication sharepointApplication in sharepointApplicationCollection)
            {
                Domain.Application application = ApplicationConverter.Convert(sharepointApplication);

                applicationCollection.Add(application);
            }

            return applicationCollection;
        }

        /// <summary>
        /// Converts the given SharePoint application to a application.
        /// </summary>
        /// <param name="sharepointApplication">
        /// The SharePoint application to be converted.
        /// </param>
        /// <returns>
        /// The result of converting <paramref name="sharepointApplication"/>.
        /// </returns>
        public static Domain.Application Convert(SPWebApplication sharepointApplication)
        {
            Domain.Application application = new Domain.Application();

            // set properties
            application.Id = sharepointApplication.Id;
            application.Name = sharepointApplication.Name;
            application.DisplayName = sharepointApplication.DisplayName;
            application.Url = sharepointApplication.GetResponseUri(SPUrlZone.Default);
            
            return application;
        }

        #endregion

    }

}
